Practice Top 30 Python Programming Questions on File and Directory Operations , TechnoVlogs

Practice Top 30 Python Programming Questions on File and Directory Operations


Q1. Use the os module to create a new directory named new_folder in the current working directory.  

Expected Output:  
Directory 'new_folder' created.

Q2. Use the os module to list all files in the current directory.  

Expected Output:  
['file1.txt', 'file2.py', 'image.jpg']

Q3. Use the os module to delete a file named file_to_delete.txt.  

Expected Output:  
File 'file_to_delete.txt' deleted.

Q4. Use the os module to rename a file old_name.txt to new_name.txt.  

Expected Output:  
File renamed from 'old_name.txt' to 'new_name.txt'.

Q5. Use the os.path module to check if a file named check_file.txt exists in the current directory.  

Expected Output:  
The file 'check_file.txt' exists.

Q6. Use the os.path module to check if a directory named test_directory exists in the current working directory.  

Expected Output:  
The directory 'test_directory' exists.

Q7. Use the shutil module to copy a file source.txt to destination.txt.  

Expected Output:  
File copied from 'source.txt' to 'destination.txt'.

Q8. Use the shutil module to move a file source.txt from the current directory to the new_folder directory.  

Expected Output:  
File 'source.txt' moved to 'new_folder/source.txt'.

Q9. Use the open() function to create a new file named new_file.txt and write "Hello, world!" to it.  

Expected Output:  
File 'new_file.txt' created and written to.

Q10. Use the os.rmdir() function to delete an empty directory named empty_folder.  

Expected Output:  
Directory 'empty_folder' deleted.

Q11. Use the os.getcwd() function to get the current working directory.  

Expected Output:  
Current directory is '/home/user/projects'.

Q12. Use the os module to list all directories in a given path.  

Input:  
'/home/user'  
 
Expected Output:  
['projects', 'documents', 'downloads']

Q13. Use the os.path module to get the absolute path of a file file.txt in the current directory.  

Expected Output:  
Absolute path: /home/user/file.txt

Q14. Use the os.path module to check if a given path is a directory.  

Input:  

'/home/user/folder'

Expected Output:  
The path is a directory.

Q15. Use the os.path module to check if a given path is a file.  

Input:  
'/home/user/file.txt'

Expected Output:  
The path is a file.

Q16. Use the os.path module to get the size of a file named example.txt.  

Expected Output:  
File size is 1024 bytes.

Q17. Use the os.makedirs() function to create nested directories parent_folder/child_folder.  

Expected Output:  
Nested directories 'parent_folder/child_folder' created.

Q18. Use the shutil module to copy a directory source_folder to destination_folder.  

Expected Output:  
Directory 'source_folder' copied to 'destination_folder'.

Q19. Use the os and os.path modules to remove all files from a directory named target_folder.  

Expected Output:  
All files in 'target_folder' removed.

Q20. Use the os.symlink() function to create a symbolic link link.txt that points to the file original.txt.  

Expected Output:  
Symbolic link 'link.txt' created.

Q21. Use the os.path module to check if a file named image.jpg has a .jpg extension.  

Expected Output:  
The file has a '.jpg' extension.

Q22. Use the pathlib module to get the parent directory of a file document.txt.  

Expected Output:  
Parent directory: /home/user

Q23. Use the pathlib module to get the extension of a file example.py.  

Expected Output:  
File extension: .py

Q24. Use the pathlib module to check if a file test_file.txt exists.  

Expected Output:  
The path 'test_file.txt' exists.

Q25. Use the pathlib module to join two path components folder and file.txt.  

Expected Output:  
Joined path: folder/file.txt

Q26. Use the pathlib module to list all files in the current directory.  

Expected Output:  
['file1.txt', 'file2.txt', 'image.jpg']

Q27. Use the pathlib module to get the directory name from a path /home/user/docs/file.txt.  

Expected Output:  
Directory name: docs

Q28. Use the pathlib module to check if a path /home/user/file.txt is absolute.  

Expected Output:  
The path is absolute.

Q29. Use the shutil module to rename a directory old_dir to new_dir.  

Expected Output:  
Directory renamed from 'old_dir' to 'new_dir'.

Q30. Use the shutil module to move a directory old_folder to a new location new_folder_location.  

Expected Output:  
Directory 'old_folder' moved to 'new_folder_location/old_folder'.

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!